package raft.rpc;

import com.alipay.remoting.exception.RemotingException;
import raft.exception.RaftRemotingException;

public class DefaultRpcClient implements RpcClient{

    private final static com.alipay.remoting.rpc.RpcClient CLIENT = new com.alipay.remoting.rpc.RpcClient();

    static {
        CLIENT.init();
        System.out.println("CLIENT init...");
    }

    @Override
    public Response send(Request request) {
        return send(request,200000);
    }

    @Override
    public Response send(Request request, int timeout) {

        Response res = null;

        try {
            res = (Response) CLIENT.invokeSync(request.getUrl(),request,timeout);
        }catch (RemotingException e){
            e.printStackTrace();
            throw new RaftRemotingException("Raft RPC Exception...");
        }
        catch (Exception E){
            E.printStackTrace();
        }

        return res;
    }

    public static void main(String[] args) {
        DefaultRpcClient d = new DefaultRpcClient();
    }

}
